package zj.xinxin.web.controller.security;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zj.xinxin.api.moudle.security.api.ConsumerService;
import zj.xinxin.api.moudle.security.dto.ConsumerInfoDto;
import zj.xinxin.api.moudle.security.vo.UserInfoSearchVo;
import zj.xinxin.api.moudle.security.vo.UserInfoUpdateVo;
import zj.xinxin.api.moudle.security.vo.UserInfoVo;
import zj.xinxin.common.annotation.Log;
import zj.xinxin.common.constant.LogConst;
import zj.xinxin.common.result.PageResult;
import zj.xinxin.common.result.ResponseResult;

import javax.validation.Valid;

/**
 * @author junzhou
 * @date 2022/4/12 17:05
 * @since 1.8
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "用户信息管理")
public class AdminUserController {

    @Autowired
    ConsumerService consumerService;

    @ApiOperation(value = "获取当前用户角色下的用户信息")
    @Log(logType = LogConst.LOG_TYPE_QUERY)
    @GetMapping("/getUserList")
    public ResponseResult<PageResult<ConsumerInfoDto>> getUserInfoList(Long current, Long size) {
        PageResult<ConsumerInfoDto> page = consumerService.getListByCurrentRoleId(current, size);
        return ResponseResult.success("查询成功！", page);
    }

    @ApiOperation(value = "查询用户信息")
    @Log(logType = LogConst.LOG_TYPE_QUERY)
    @GetMapping("/search")
    public ResponseResult<PageResult<ConsumerInfoDto>> searchUserInfo(UserInfoSearchVo searchVo) {
        PageResult<ConsumerInfoDto> page = consumerService.searchUserInfo(searchVo);
        return ResponseResult.success("查询成功！", page);
    }

    @ApiOperation(value = "添加用户")
    @Log(logType = LogConst.LOG_TYPE_SAVE)
    @PostMapping("/add")
    public ResponseResult<Boolean> addUser(@Valid @RequestBody UserInfoVo userInfo) {

        if (userInfo.getRoleId() == null) {
            return ResponseResult.fail("角色 Id 不能为空! ", false);
        }
        Boolean result = consumerService.addUser(userInfo);

        return result ? ResponseResult.success("添加用户成功! ", true)
                : ResponseResult.fail("添加用户失败!", false);
    }

    @ApiOperation(value = "更新用户信息")
    @Log(logType = LogConst.LOG_TYPE_UPDATE)
    @PutMapping("/update")
    public ResponseResult<Boolean> updateUser(@RequestBody
                                              @Valid UserInfoUpdateVo userInfoVo){
        Boolean res = consumerService.updateUser(userInfoVo);
        return res ? ResponseResult.success("更新用户成功! ", true)
                : ResponseResult.fail("更新用户失败！", false);
    }

    @ApiOperation(value = "删除用户信息")
    @Log(logType = LogConst.LOG_TYPE_DELETE)
    @DeleteMapping("/delete/{userId}")
    public ResponseResult<Boolean> deleteUser(@PathVariable("userId") Integer userId){
        Boolean res = consumerService.deleteUserInfo(userId);
        return res ? ResponseResult.success("删除用户信息成功! ", true)
                : ResponseResult.fail("删除用户信息失败！", false);
    }
}
